package shop.controller.front.offline;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.util.QRcodeUtils;
import shop.services.front.account.bean.Account;
import shop.services.front.offline.OfflineService;
import shop.services.front.offline.bean.Offline;
import shop.services.front.offlineAccount.OfflineAccountService;
import shop.services.front.offlineAccount.bean.OfflineAccount;
import shop.services.front.organize.OrganizeService;
import shop.services.front.organize.bean.Organize;
import shop.services.manage.system.bean.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by wzl on 2017/7/7.
 */
@Controller
@RequestMapping("/front/offline")
public class OfflineController extends FrontBaseController<Offline> {
    @Resource
    private OfflineService offlineService;
    @Resource
    private OrganizeService organizeService;
    @Resource
    private OfflineAccountService offlineAccountService;

    @Override
    public Services<Offline> getService() {
        return offlineService;
    }


    /**
     * 跳转到活动列表页面
     *
     * @return
     */
    @RequestMapping("/toOfflineList")
    public String toOfflineList() {
        return "/front/offline/offlineList";
    }

    /**
     * 跳转到活动详情页面
     *
     * @return
     */
    @RequestMapping("/toOfflineDetail")
    public String toOfflineDetail() {
        return "/front/offline/offlineDetail";
    }

    /**
     * 跳转到发布活动页面
     *
     * @return
     */
    @RequestMapping("/toOfflineAdd")
    public String toOfflineAdd() {
        return "/front/offline/offlineAdd";
    }

    /**
     * 跳转到活动关闭详情页面
     * @return
     */
    @RequestMapping("/toOfflineClosed")
    public String toOfflineClosed() {
        return "/front/offline/offlineClosed";
    }
    /**
     * 跳转到募捐活动支付页
     * @return
     */
    @RequestMapping("/toOfflinePay")
    public String toOfflinePay() {
        return "/front/offline/offlinePay";
    }


    /**
     * 加入活动方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertOfflineAccount", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertOfflineAccount(OfflineAccount offlineAccount, HttpSession session) throws Exception {
        JSONResult jsonResult = new JSONResult();
        Offline offline = new Offline();
        offline.setId(String.valueOf(offlineAccount.getOfflineID()));
        offline = offlineService.selectOne(offline);
        Account account = (Account) session.getAttribute("accountInfo");
        offlineAccount.setOrganizeID(offline.getOrganizeID());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date2 = offline.getEndTime();
        if (account != null) {
            if(sdf.format(new Date()).compareTo(date2)<=0) {
                if (!account.getStatus().equals("1")) {
                    jsonResult.setSuccess(false);
                    jsonResult.setMessage("您还未通过审核，无法报名参加活动！");
                    return jsonResult;
                }
                if (offline.getType().equals("1")) {
                    List<OfflineAccount> offlineAccountList = offlineAccountService.selectListByOffline(offlineAccount);
                    for (OfflineAccount offlineAccount1 : offlineAccountList) {
                        if (offlineAccount1.getAccountID().equals(account.getId())) {
                            jsonResult.setSuccess(false);
                            jsonResult.setMessage("您已报名此活动，无需重复报名!");
                            return jsonResult;
                        }
                    }
                }
                if (offline.getParticipant().equals("2") && !account.getOrganizeID().equals(offline.getOrganizeID())) {//仅限内部
                    jsonResult.setSuccess(false);
                    jsonResult.setMessage("抱歉，报名失败，您不属于该活动的参与对象。");
                    return jsonResult;
                } else if (offline.getParticipant().equals("3") && account.getOrganizeID().equals(offline.getOrganizeID())) {//仅限外部
                    jsonResult.setSuccess(false);
                    jsonResult.setMessage("抱歉，报名失败，您不属于该活动的参与对象。");
                    return jsonResult;
                } else {
                    if ((offline.getAccountNum() < offline.getLimits()) || offline.getLimits() == 0) {
                        offline.setAccountNum(offline.getAccountNum() + 1);
                        offlineService.update(offline);
                        offlineAccount.setAccountID(account.getId());
                        int id = offlineAccountService.insert(offlineAccount);
                        jsonResult.setData(id);
                        jsonResult.setSuccess(true);
                        return jsonResult;
                    } else {
                        jsonResult.setSuccess(false);
                        jsonResult.setMessage("抱歉，报名失败，报名人数超出上限人数。");
                        return jsonResult;
                    }
                }
            }else{
                jsonResult.setMessage("活动已结束，期待您的下次参与！");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("您还未登录，请先登录");
            return jsonResult;
        }
    }

    /**
     * 发布活动
     *
     * @param request
     * @param offline
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/insertJsons", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertJsons(Offline offline,HttpServletRequest request) {
        jsonResult = new JSONResult();
        User user = (User) request.getSession().getAttribute("userInfo");
        Organize organize = new Organize();
        organize.setPrincipalPhone(user.getUsername());
        organize = organizeService.selectOne(organize);
        offline.setOrganizeID(organize.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String date1 = sdf.format(new Date(date.getTime() + 1 * 24 * 60 * 60 * 1000));
        String date2 = sdf.format(new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000));
        String date3 = offline.getBeginTime();
        if(organize.getStatus().equals("1")){
            if (offline.getType().equals("1") && offline.getSend().equals("n")) {
                if(date3.compareTo(date1)>=0){
                    QRcodeUtils qrCodeUtils = new QRcodeUtils();
                    String contents = "contents";
                    offline.setQrCode(qrCodeUtils.createQrcode(request, contents));
                    offline.setStatus("2");
                    getService().insert(offline);
                    organize.setOfflineNum(organize.getAccountNum() + 1);
                    organizeService.update(organize);
                    return jsonResult;
                }else{
                    jsonResult.setMessage("请提前一天发布活动！");
                    jsonResult.setSuccess(false);
                    return jsonResult;
                }
            } else if (offline.getType().equals("1") && offline.getSend().equals("y")) {
                if (date3.compareTo(date2) > 0) {
                    offline.setStatus("1");
                    getService().insert(offline);
                    organize.setOfflineNum(organize.getAccountNum() + 1);
                    organizeService.update(organize);
                    return jsonResult;
                } else {
                    jsonResult.setMessage("请提前七天发布活动！");
                    jsonResult.setSuccess(false);
                    return jsonResult;
                }
            } else {
                if (date3.compareTo(date2) > 0) {
                    offline.setStatus("1");
                    getService().insert(offline);
                    organize.setOfflineNum(organize.getAccountNum() + 1);
                    organizeService.update(organize);
                    return jsonResult;
                } else {
                    jsonResult.setMessage("请提前七天发布活动！");
                    jsonResult.setSuccess(false);
                    return jsonResult;
                }
            }
        }else{
            jsonResult.setMessage("您还未通过审核，无法发布活动！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }


    @RequestMapping("/deleteAccountOfflineById")
    @ResponseBody
    public JSONResult deleteAccountOfflineById(OfflineAccount offlineAccount){
        jsonResult = new JSONResult();
        offlineAccountService.delete(offlineAccount);
        jsonResult.setSuccess(true);
        return jsonResult;
    }


    @RequestMapping(value = "/selectSendOffline",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectSendOffline(HttpServletRequest request,Offline offline){
    jsonResult=new JSONResult();
    int offset = 0;
    int pageSize = 10;
    if (request.getParameter("start") != null) {
        offset = Integer.parseInt(request.getParameter("start"));
    }
    if (request.getParameter("length") != null) {
        pageSize = Integer.parseInt(request.getParameter("length"));
    }
    if (offset < 0)
        offset = 0;
    if (pageSize < 0) {
        pageSize = 10;
    }
    offline.setOffset(offset);
    offline.setPageSize(pageSize);
        List<Offline> offlineList=offlineService.selectSendOffline(offline);
    jsonResult.setData(offlineList);
    jsonResult.setSuccess(true);
    return jsonResult;
}
}
